<template>
  <div>
    <!-- 表格部分 -->
    <el-table
      ref="singleTable"
      v-loading="listLoading"
      :data="list"
      :stripe="true"
      fit
      highlight-current-row
      :height="tableHeight"
      :header-cell-style="{background:'#eef1f6',color:'#606266'}"
      @current-change="handleCurrentChange"
    >
      <template slot="empty">
        <tableEmpty />
      </template>
      <el-table-column
        label="序号"
        prop="no"
        align="center"
        width="80"
      />
      <el-table-column
        label="生成状态"
        align="center"
      >
        <template slot-scope="{row}">
          <span>{{ row.status | statusFilter() }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="单位编码"
        prop="agencyCode"
        align="center"
      />
      <el-table-column
        label="单位名称"
        prop="agencyName"
        align="center"
      />
      <el-table-column
        label="月份"
        prop="month"
        align="center"
      />
      <el-table-column
        label="应发工资"
        prop="shouldSalary"
        align="center"
      />
      <el-table-column
        label="实发工资"
        prop="realSalary"
        align="center"
      />
    </el-table>
    <!--分页部分-->
    <div class="size">
      <pagination
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />
    </div>
    <el-row v-if="tabPane === '1'">
      <el-col :span="4">
        <span style="margin: 10px; display: inline-block;">人员工资明细列表:</span>
      </el-col>
      <el-col :offset="16" :span="4">
        <el-button
          size="small"
          class="filter-item"
          style="margin:10px; float: right;"
          type="primary"
          icon="el-icon-full-screen"
          @click="fullscreen"
        />
      </el-col>
    </el-row>
    <!-- 表格部分 -->
    <el-table
      v-if="tabPane === '1'"
      ref="personTable"
      v-loading="personListLoading"
      :data="personList"
      :stripe="true"
      fit
      :height="tableHeight"
      style="width: 100%;"
      :header-cell-style="{background:'#eef1f6',color:'#606266'}"
    >
      <template slot="empty">
        <tableEmpty />
      </template>
      <el-table-column
        label="序号"
        prop="index"
        align="center"
        width="80"
        fixed
      />
      <el-table-column
        label="工资月份"
        prop="currentMonth"
        align="center"
        fixed
      />
      <el-table-column
        label="科室"
        prop="deptId"
        align="center"
        fixed
      >
        <template slot-scope="{row}">
          <span>{{ row.deptId | deptNameFilter(that) }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="身份证号"
        prop="idenNo"
        align="center"
        min-width="180"
        fixed
      >
       <template slot-scope="{row}">
          <span>{{ row.idenNo  }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="姓名"
        prop="personName"
        align="center"
        fixed
      />
      <el-table-column
        v-for="item in columnNames"
        :key="item.split('_')[0]"
        :label="item.split('_')[0] | salaryNameFilter(that)"
        align="center"
        min-width="150"
      >
        <template slot-scope="{row}">
          <div style="float: right;">
            <span>{{ Number(row[item.split('_')[0]]).toFixed(2) }}</span>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <!--分页部分-->
    <div class="size">
      <pagination
        v-if="tabPane === '1'"
        :total="personTotal"
        :page.sync="personListQuery.page"
        :limit.sync="personListQuery.limit"
        @pagination="getPersonList"
      />
    </div>
    <!--  全屏  -->
    <div v-if="isFullScreen" class="full-screen">
      <el-row v-if="tabPane === '1'">
        <el-col :span="4">
          <span style="margin: 10px; display: inline-block;">人员工资明细列表:</span>
        </el-col>
        <el-col :offset="16" :span="4">
          <el-button
            size="small"
            class="filter-item"
            style="margin:10px; float: right;"
            type="primary"
            icon="el-icon-full-screen"
            @click="exitFullscreen"
          />
        </el-col>
      </el-row>
      <!-- 表格部分 -->
      <el-table
        v-if="tabPane === '1'"
        v-loading="personListLoading"
        :data="personList"
        :stripe="true"
        fit
        :height="tableFullScreenHeight"
        style="width: 100%;"
        :header-cell-style="{background:'#eef1f6',color:'#606266'}"
      >
        <template slot="empty">
          <tableEmpty />
        </template>
        <el-table-column
          label="序号"
          prop="index"
          align="center"
          width="80"
          fixed
        />
        <el-table-column
          label="工资月份"
          prop="currentMonth"
          align="center"
          fixed
        />
        <el-table-column
          label="科室"
          prop="deptId"
          align="center"
          fixed
        >
          <template slot-scope="{row}">
            <span>{{ row.deptId | deptNameFilter(that) }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="身份证号"
          prop="idenNo"
          align="center"
          min-width="180"
          fixed
        >
        <template slot-scope="{row}">
            <span>{{ row.idenNo  }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="姓名"
          prop="personName"
          align="center"
          fixed
        />
        <el-table-column
          v-for="item in columnNames"
          :key="item.split('_')[0]"
          :label="item.split('_')[0] | salaryNameFilter(that)"
          align="center"
          min-width="150"
        >
          <template slot-scope="{row}">
            <div style="float: right;">
              <span>{{ Number(row[item.split('_')[0]]).toFixed(2) }}</span>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!--分页部分-->
      <div class="size">
        <pagination
          v-if="tabPane === '1'"
          :total="personTotal"
          :page.sync="personListQuery.page"
          :limit.sync="personListQuery.limit"
          @pagination="getPersonList"
        />
      </div>
    </div>
  </div>
</template>

<script>
import {
  queryColumnName,
  querySalaryNameDict
} from '@/api/unitSalaryReport/unitSalaryReport'
import {
  queryGenerateBankDataPage,
  queryPersonSalaryInfoPage,
  generateAndExportBankData,
  revokeGenerate
} from '@/api/generateSalaryData/generateBankData'
import { getDepartmentList } from '@/api/unitInformationMaintenance/internalOrganizationMaintenance'
import Pagination from '@/components/Pagination'
import tableEmpty from '@/components/table/empty'

export default {
  name: 'IsGenerateBankDataChildren',
  components: {
    Pagination,
    tableEmpty
  },
  filters: {
    deptNameFilter(value, that) {
      const deptNames = that.deptNames
      for (const item of deptNames) {
        if (value === item.code) {
          return item.name
        }
      }
    },
    // 工资项滤器
    salaryNameFilter(value, that) {
      const salaryNames = that.salaryNames
      for (const item of salaryNames) {
        if (item.value === value) {
          return item.key
        }
      }
    },
    // 生成状态
    statusFilter(type) {
      if (type === '0') {
        return '未生成'
      } else if (type === '1') {
        return '已生成'
      }
    },
    //号码脱敏显示
    hideMiddle(val) {
      if(val!== undefined&& val!== null){//避免列表中存在空数据报错
        return `${val.substring(0,4)}****${val.substring(val.length-4)}`
      }else{
        return val
      }
    }
  },
  props: ['tabPane'],
  data() {
    return {
      isFullScreen: false,
      tableHeight: null,
      tableFullScreenHeight: null,
      treeShow: false,
      that: this,
      listLoading: true,
      list: [],
      total: 0,
      listQuery: {
        page: 1,
        limit: 500
      },
      agencyId: '',
      salaCardBank: '',
      columnNames: [], // 动态表头
      salaryNames: [],
      deptNames: [],
      personListLoading: true,
      personList: [],
      personTotal: 0,
      personListQuery: {
        page: 1,
        limit: 200
      }
    }
  },
  watch: {
    agencyId(newValue, oldValue) {
      if (newValue && this.tabPane === '1') {
        this.getColumnNames(newValue)
        this.getSalaryName(newValue)
        this.getDepartment(newValue)
        this.handlePersonFilter(newValue)
      }
    },
    list(val) {
      this.doLayout()
    },
    personList(val) {
      this.doPersonLayout()
    }
  },
  mounted() {
    this.getTbHeight()
    this.getTbFullScreenHeight()
  },
  beforeMount() {
    window.addEventListener('resize', this.getTbHeight)
    window.addEventListener('resize', this.getTbFullScreenHeight)
  },
  created() {
    this.agencyId = ''
    this.listQuery.tabPane = this.tabPane
    this.handleFilter()
  },
  methods: {
    doLayout() {
      this.$nextTick(() => {
        this.$refs.singleTable.doLayout()
      })
    },
    doPersonLayout() {
      this.$nextTick(() => {
        this.$refs.personTable.doLayout()
      })
    },
    getTbHeight() {
      if (this.tabPane === '1') {
        this.tableHeight = window.innerHeight / 2 - 166
      } else {
        this.tableHeight = window.innerHeight - 228
      }
    },
    getTbFullScreenHeight() {
      this.tableFullScreenHeight = window.innerHeight - 140
    },
    handleFilter() {
      this.listQuery.page = 1
      // this.listQuery.limit = 10
      this.listQuery.agencyId = this.agencyId
      this.getList()
    },
    handlePersonFilter(newValue) {
      this.personListQuery.page = 1
      this.personListQuery.limit = 200
      this.personListQuery.agencyId = newValue
      this.getPersonList()
    },
    getList() {
      this.listLoading = true
      queryGenerateBankDataPage(this.listQuery).then(res => {
        this.list = res.data.rows
        this.total = res.data.total
        this.listLoading = false
        if (this.list.length > 0) {
          this.setCurrent(true)
        } else {
          this.setCurrent(false)
          this.agencyId = ''
          this.personList = []
          this.personTotal = 0
          this.personListLoading = false
        }
      })
    },
    getPersonList(newValue) {
      this.personListLoading = true
      queryPersonSalaryInfoPage(this.personListQuery).then(res => {
        this.personList = res.data.rows
        this.personTotal = res.data.total
        this.personListLoading = false
      })
    },
    getSalaryName(agencyId) {
      this.personListQuery.agencyId = agencyId
      // 查询工资项名称
      querySalaryNameDict(this.personListQuery).then(res => {
        this.salaryNames = res.data
      })
    },
    getColumnNames(agencyId) {
      this.personListQuery.agencyId = agencyId
      // 查询工资项字段
      queryColumnName(this.personListQuery).then(res => {
        this.columnNames = res.data
      })
    },
    getDepartment(agencyId) {
      // 查询部门名称
      getDepartmentList({ agencyId: agencyId }).then(res => {
        this.deptNames = res.data
      })
    },
    generateAndExportBankData() {
      // this.$confirm('确定生成吗？', '提示', {
      //   closeOnClickModal: false,
      //   confirmButtonText: '确定',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // }).then(() => {
      generateAndExportBankData().then(res => {
        const link = document.createElement('a')
        const objectUrl = URL.createObjectURL(res)
        link.href = objectUrl
        link.download = '银行数据.xls'
        link.click()
        URL.revokeObjectURL(objectUrl)
        this.handleFilter()
        this.$emit('handleGenerateBtnLoading')
      }).catch(() => {
        this.$emit('handleGenerateBtnLoading')
      })
      // }).catch(() => {
      //   this.$emit('handleGenerateBtnLoading')
      // })
    },
    //撤销生成
    regenerate(){
        this.$confirm('此操作将撤销生成, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
         revokeGenerate().then(response => {
            this.$message({
              showClose: true,
              message: '撤销生成成功',
              type: 'success',
              duration: 4000
            })
            this.handleFilter()
            this.$emit('revokeGenerateSuccess')
       })
      })
    },
    // 设置初始选中行
    setCurrent(flag) {
      if (this.tabPane === '1' && flag) {
        this.$refs.singleTable.setCurrentRow(this.list[1])
      } else {
        this.$refs.singleTable.setCurrentRow()
      }
    },
    // 选中行
    handleCurrentChange(row) {
      if (row) {
        this.agencyId = row.agencyId
      }
    },
    // 树的点击事件
    handleTree(data) {
      this.agencyId = data.id
      this.handleFilter()
    },
    // 切换树是否显示
    toggerTree() {
      this.treeShow = !this.treeShow
    },
    query(salaCardBank) {
      this.listQuery.salaCardBank = salaCardBank
      this.personListQuery.salaCardBank = salaCardBank
      this.getList()
      this.getPersonList()
    },
    fullscreen() {
      this.isFullScreen = true
    },
    exitFullscreen() {
      this.isFullScreen = false
    }
  }
}
</script>

<style lang="scss" scoped>
// .size.el-pagination.is-background .btn-prev,
// .el-pagination.is-background .btn-next {
//   width: 15px !important;
// }
  .full-screen {
    z-index: 9999;
    background: #fff;
    position: fixed;
    padding: 20px;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    height: 100%;
    width: 100%;
  }
</style>
